package com.logicwin.RobotTravel.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;

public class Graphics
{
	public static void fillRect(Canvas g, Rect rect, Paint paint)
	{
		paint.setStyle(Paint.Style.FILL);
		g.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
	}

	public static void drawRect(Canvas g, Rect rect, Paint paint)
	{
		paint.setStyle(Paint.Style.STROKE);
		g.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
	}


	public static void SETAEERECT(Rect rect, int x, int y, int w, int h)
	{
		rect.left = x;
		rect.top = y;
		rect.right = x + w;
		rect.bottom = y + h;
	}


	public static void fillRect(Canvas g, int x, int y, int w, int h, Paint paint)
	{
		paint.setStyle(Paint.Style.FILL);
		g.drawRect(x, y, x + w, y + h, paint);
	}


	public static void drawRect(Canvas g, int x, int y, int w, int h, Paint paint)
	{
		paint.setStyle(Paint.Style.STROKE);
		g.drawRect(x, y, x + w, y + h, paint);
	}


	public static void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by)
	{
		Rect src = new Rect();
		Rect dst = new Rect();
		src.left = bx;
		src.top = by;
		src.right = bx + w;
		src.bottom = by + h;
		dst.left = x;
		dst.top = y;
		dst.right = x + w;
		dst.bottom = y + h;
		canvas.drawBitmap(blt, src, dst, null);

		src = null;
		dst = null;
	}

	public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y)
	{
		canvas.drawBitmap(bitmap, x, y, null);
	}
	
	public static void drawTextInRect(Canvas canvas, String strText, int dstX, int dstY,
			int width, int height, int fontSize, int color)
	{
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		p.setTypeface(font);		
		p.setColor(color);		
		//p.setFakeBoldText(true);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		p.setTextSize(fontSize);
		
		int charWidth = fontSize;
		int charHeight = fontSize + fontSize/2;
		int nLines = height/(fontSize*2);
		int nCharsOneLine = width/charWidth;
		int nTotalChars = nLines*nCharsOneLine;
		for(int i=0; i< strText.length() && i< nTotalChars ; i++)
		{
			int nX = i%nCharsOneLine;
			int nY = i/nCharsOneLine;
			drawChar(canvas, strText.charAt(i), dstX + nX*charWidth, 
					dstY+ nY*charHeight+ fontSize, p);
		}
	}
	
	private static void drawChar(Canvas canvas, char text, int x, int y, Paint p)
	{
		canvas.drawText(""+text,x,y,p);
	}
	
	public static void drawText(Canvas canvas, String strText, int x, int y, 
			int fontSize, int color, String fontType)
	{
		Paint p = new Paint();
		if (fontType != null)
		{
			Typeface font = Typeface.create(fontType,Typeface.BOLD);
			p.setTypeface(font);
		}
		
		p.setColor(color);		
		p.setTextSize(fontSize);
		
		canvas.drawText(strText,x,y,p);		
	}
	/*
	# 函数：创建字体(Create())，获取字体属性(getStyle()、isBold()、isItalic())；
	Typeface类不仅定义了字体，还包括粗体（Bold）、斜体（Italic）。
	
	Paint.setARGB(this.mAlpha, Color.red(this.mFontColor), Color
				.green(this.mFontColor), Color.blue(this.mFontColor));
	//消除锯齿	
	paint.setFlags(Paint.ANTI_ALIAS_FLAG)
	//取得字符串宽度  
	paint.measureText()  				
	*/
	public static void drawTextCenter(Canvas canvas, String strText, int left, int width, 
			int y,int fontSize, int color)
	{
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		p.setTypeface(font);		
		p.setColor(color);		
		//p.setFakeBoldText(true);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		p.setTextSize(fontSize);
		int textwidth = (int)p.measureText(strText);
		if(textwidth > width)
			canvas.drawText(strText,left ,y,p);	
		else
			canvas.drawText(strText,left + (width - textwidth)/2,y,p);				
	}
	
	public static int getTextSize(String strText, int fontSize, int color)
	{
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		p.setTypeface(font);		
		p.setColor(color);		
		//p.setFakeBoldText(true);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		p.setTextSize(fontSize);
		return (int)p.measureText(strText);
	}
	
	public static void drawText(Canvas canvas, String strText, int x, int y, 
			int fontSize, int color)
	{
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		p.setTypeface(font);		
		p.setColor(color);		
		//p.setFakeBoldText(true);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		p.setTextSize(fontSize);
		
		canvas.drawText(strText,x,y,p);		
	}	
	
	public static void drawText(Canvas canvas, String strText, int x, int y, 
			int fontSize)
	{
		//LwLog.info("Text position is " + x + "'" + y);
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		//Typeface font = Typeface.create("宋体",Typeface.NORMAL);
		p.setTypeface(font);		
		p.setARGB(255, 65, 35, 8);		
		p.setTextSize(fontSize);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		canvas.drawText(strText,x,y,p);		
	}		
	
	public static void drawText(Canvas canvas, char chText, int x, int y, 
			int fontSize, int color)
	{
		String strText =""; 
		strText += chText;
		Paint p = new Paint();
		Typeface font = Typeface.create("宋体",Typeface.BOLD);
		p.setFlags(Paint.ANTI_ALIAS_FLAG);
		p.setTypeface(font);		
		p.setColor(color);		
		p.setTextSize(fontSize);
		
		canvas.drawText(strText,x,y,p);	
	}	
	public static void drawHText(Canvas canvas, String strText, int x, int y, int fontSize, int color)
	{
		drawText(canvas, strText,x,y,fontSize, color);
	}
	public static void drawVText(Canvas canvas, String strText, int x, int y,int fontSize, int color)
	{
		if(strText == null) return;
		for(int i=0; i<strText.length(); i++)
			drawText(canvas, strText.charAt(i), x, y + i*fontSize, fontSize, color);
	}
}

